package com.cqtd.menu.action;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSON;
import com.cqtd.base.action.FooGenericAction;
import com.cqtd.base.pojo.FooGenericSearch;
import com.cqtd.base.pojo.FooGenericSearchResult;
import com.cqtd.menu.model.MenuSolution;
import com.cqtd.menu.model.Module;
import com.cqtd.util.FooUtils;
import com.cqtd.util.StaticMethod;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;

@Controller
@RequestMapping("/menu/menuAction")
public class MenuAction extends FooGenericAction{

	@RequestMapping("/getMenuSolustion")
	public void getMenuSolustion(HttpServletRequest request,HttpServletResponse response) {
		
		//从SESSION中取出用户信息，拿到菜单的方案ID
		String menuSolutionId = "0";
		//从菜单方案表中查询出该具体的菜单方案
		String hql = "from MenuSolution where id=:id";
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("id", menuSolutionId);
		FooGenericSearch fooGenericSearch = new FooGenericSearch();
		fooGenericSearch.setQueryHql(hql);
		FooGenericSearchResult fooGenericSearchResult = fooGenericService.search(fooGenericSearch, params);
		MenuSolution ms = (MenuSolution) fooGenericSearchResult.getResult().get(0);
		
		Map<String, Object> map = Maps.newHashMap();
		map.put("westMenuJson", ms.getWestMenuJson());
		map.put("centerMenuJson", ms.getCenterMenuJson());

		try {
			FooUtils.printJsonObject(response, map);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	
	@RequestMapping("/getMenuSolustion2")
	public ModelAndView getMenuSolustion2(HttpServletRequest request,HttpServletResponse response) {
		ModelAndView mav = new ModelAndView();
		//从SESSION中取出用户信息，拿到菜单的方案ID
		String menuSolutionId = "0";
		//从菜单方案表中查询出该具体的菜单方案
		String hql = "from MenuSolution where id=:id";
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("id", menuSolutionId);
		FooGenericSearch fooGenericSearch = new FooGenericSearch();
		fooGenericSearch.setQueryHql(hql);
		FooGenericSearchResult fooGenericSearchResult = fooGenericService.search(fooGenericSearch, params);
		MenuSolution ms = (MenuSolution) fooGenericSearchResult.getResult().get(0);
		mav.addObject("westMenuJson", JSON.toJSONStringWithDateFormat(ms.getWestMenuJson(),"yyyy-MM-dd HH:mm:ss"));
		mav.addObject("centerMenuJson", JSON.toJSONStringWithDateFormat(ms.getCenterMenuJson(),"yyyy-MM-dd HH:mm:ss"));
		mav.setViewName("/mis/index");
		return mav;
	}
	
	
	
	@RequestMapping("/menulist4")
	@ResponseBody
	public void menulist4(HttpServletResponse response) {

		String hql = "from Module";
		String countHql = "select count(id) " + hql;

		Map<String, Object> map = Maps.newHashMap();

		FooGenericSearch fooGenericSearch = new FooGenericSearch();
		fooGenericSearch.setQueryHql(hql);
		fooGenericSearch.setPage(1);
		fooGenericSearch.setRows(15);
		fooGenericSearch.setCountHql(countHql);
		FooGenericSearchResult fooGenericSearchResult = fooGenericService
				.searchAndCount(fooGenericSearch);

		map.put("items", fooGenericSearchResult.getResult());
		map.put("total", fooGenericSearchResult.getTotalCount());

		try {
			FooUtils.printJsonObject(response, map);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	@RequestMapping("/menulist3")
	@ResponseBody
	public void menulist3(HttpServletResponse response) {

		String hql = "from Module";
		String countHql = "select count(id) " + hql;

		FooGenericSearch fooGenericSearch = new FooGenericSearch();
		fooGenericSearch.setQueryHql(hql);
		fooGenericSearch.setPage(1);
		fooGenericSearch.setRows(15);
		fooGenericSearch.setCountHql(countHql);
		FooGenericSearchResult fooGenericSearchResult = fooGenericService
				.searchAndCount(fooGenericSearch);

		Map<String, Object> map = Maps.newHashMap();
		map.put("datas", fooGenericSearchResult.getResult());
		map.put("total", fooGenericSearchResult.getTotalCount());

		try {
			FooUtils.printJsonObject(response, map);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	@SuppressWarnings("unchecked")
	@RequestMapping("/menulistAsyn")
	@ResponseBody
	public void menulistAsyn(HttpServletRequest request,
			HttpServletResponse response) {
		
		
		/**
		 * 
		 * ext分页的两个参数是start 当前页,limit 每页显示条数 
		 *  int start = Integer.parseInt(req.getParameter("start"));
      	 *	int limit = Integer.parseInt(req.getParameter("limit"));
      	 *
		 */
		
		
		List<Module> result = Lists.newArrayList();
		String node = StaticMethod
				.nullObject2String(request.getParameter("id"));
		String hql = "from Module where parentId=:parentId";
		Map<String, Object> params = new HashMap<String, Object>();
		if (node.equals("root") || "".equals(node)) {
			node = "";
		}
		params.put("parentId", node);

		FooGenericSearch fooGenericSearch = new FooGenericSearch();
		fooGenericSearch.setQueryHql(hql);
		result = (List<Module>) fooGenericService.search(fooGenericSearch,params)
				.getResult();

		Map<String, Object> map = Maps.newHashMap();
		map.put("models", result);

		try {
			FooUtils.printJsonObject(response, map);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	@RequestMapping("/menulist")
	@ResponseBody
	public void menulist(HttpServletResponse response) {
		// [{"text":"备案列表","id":"4","iconCls":"Folder","stores":"bastore","columns":[{text:'序号',dataIndex:'ID'},{text:'公司名称',dataIndex:'kehu_name'}],"leaf":true},{"text":"新增备案","id":"5","iconCls":"Folder","stores":"","columns":[],"leaf":true}]
		String res = "[{\"text\":\"备案列表\",\"id\":\"4\",\"iconCls\":\"Folder\",\"stores\":\"bastore\",\"columns\":[{text:'序号',dataIndex:'ID',width:50},{text:'公司名称',dataIndex:'kehu_name',width:260,editor:{allowBlank: false}},{text:'备案号',dataIndex:'beianhao',width:140,editor:{allowBlank: false}},{text:'备案密码',dataIndex:'beianpass',width:100,editor:{allowBlank: false}},{text:'备案邮箱',dataIndex:'beianemail',width:160,editor:{allowBlank: false}},{text:'备案邮箱密码',dataIndex:'emailpass',width:120,editor:{allowBlank: false}},{text:'备案账号',dataIndex:'beianzh',width:160,editor:{allowBlank: false}},{text:'账号密码',dataIndex:'beianzhpa',width:120,editor:{allowBlank: false}} ],\"leaf\":true},{\"text\":\"新增备案\",\"id\":\"5\",\"iconCls\":\"Folder\",\"stores\":\"test\",\"columns\":[{text:'序号',dataIndex:'ID',width:50},{text:'公司名称',dataIndex:'kehu_name',width:260,editor:{allowBlank: false}},{text:'备案号',dataIndex:'beianhao',width:140,editor:{allowBlank: false}},{text:'备案密码',dataIndex:'beianpass',width:100,editor:{allowBlank: false}},{text:'备案邮箱',dataIndex:'beianemail',width:160,editor:{allowBlank: false}},{text:'备案邮箱密码',dataIndex:'emailpass',width:120,editor:{allowBlank: false}},{text:'备案账号',dataIndex:'beianzh',width:160,editor:{allowBlank: false}},{text:'账号密码',dataIndex:'beianzhpa',width:120,editor:{allowBlank: false}} ],\"leaf\":true}]";
		Object object = JSON.parse(res);
		try {
			FooUtils.printJsonObject(response, object);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	@RequestMapping("/getgridlist")
	@ResponseBody
	public void getgridlist(HttpServletResponse response) {
		// fields: ['ID',
		// 'kehu_name','beianhao','beianpass','beianemail','emailpass','beianzh','beianzhpa']
		String items = "[{\"ID\":\"1\",\"kehu_name\":\"colin\",\"beianhao\":\"NO001\",\"beianpass\":\"123\",\"beianemail\":\"colin@126.com\",\"emailpass\":\"123\",\"beianzh\":\"test\",\"beianzhpa\":\"123\"},{\"ID\":\"2\",\"kehu_name\":\"jack\",\"beianhao\":\"NO002\",\"beianpass\":\"456\",\"beianemail\":\"jack@163.com\",\"emailpass\":\"456\",\"beianzh\":\"cheshi\",\"beianzhpa\":\"456\"}]";
		Map<String, Object> map = Maps.newHashMap();
		map.put("items", JSON.parse(items));
		map.put("total", "2");
		try {
			FooUtils.printJsonObject(response, map);
			logger.info("----getgridlist-----");
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	@RequestMapping("/save")
	@ResponseBody
	public void save(HttpServletResponse response) {
		try {
			Module m = new Module();
			m.setLeaf("true");
			m.setIconCls("icon-menu");
			m.setMgrUrl("menu.menuGrid");
			m.setOrderNo(0);
			m.setParentId("");
			m.setText("菜单管理");
			fooGenericService.save(m);

			Module m2 = new Module();
			m2.setLeaf("true");
			m2.setIconCls("icon-menu");
			m2.setMgrUrl("menu.menuGrid");
			m2.setOrderNo(1);
			m2.setParentId("402881e63dd88f04013dd8900ba10000");
			m2.setText("菜单管理");
			fooGenericService.save(m);
			FooUtils.printJsonSuccessMsg(response);
			logger.info("----save-----");
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	@RequestMapping("/del")
	@ResponseBody
	public void del(HttpServletResponse response) {
		try {
			FooUtils.printJsonSuccessMsg(response);
			logger.info("----del-----");
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

}
